﻿Imports System.IO

Public Class Form1
    Public ViewedItem As New ArrayList
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CheckForIllegalCrossThreadCalls = False
        'Load Settings
        Try
            If File.Exists("default.txt") Then
                Dim values As String = File.ReadAllText("default.txt")
                Dim ValueList As String()
                ValueList = values.Split(vbNewLine)
                For Each iten In ValueList
                    Dim SplitValues As String() = iten.Split("|")
                    If SplitValues(0) = "Connection_String" Then
                        My.Settings.con = SplitValues(1)
                    ElseIf SplitValues(0) = "File_Protocol" Then
                        My.Settings.fileprotocol = SplitValues(1)
                    ElseIf SplitValues(0) = "FTP_Username" Then
                        My.Settings.ftpusername = SplitValues(1)
                    ElseIf SplitValues(0) = "FTP_Password" Then
                        My.Settings.ftppassword = SplitValues(1)
                    End If
                    My.Settings.Save()
                Next
                File.Delete("default.txt")
            End If
        Catch ex As Exception

        End Try


        categorylist.SelectedIndex = 0
        If My.Settings.username = "" Then
            LoginForm1.MainForm = Me
            LoginForm1.ShowDialog()
        End If

        upload.MainForm = Me

        Dim loaditemsinthread As New Threading.Thread(AddressOf LoadItems)
        loaditemsinthread.Start()


        FillAutoCompleteUserItems()
        Dim LoadVendor As New Threading.Thread(AddressOf FillAutoCompleteVendor)
        LoadVendor.Start()
        'FillAutoCompleteVendor()

        Dim InitializeDocType As New Threading.Thread(AddressOf InitDocType)
        InitializeDocType.Start()

    End Sub
    Public Doctypes As New ArrayList
    Sub InitDocType()
        Try
            Dim SQLCommand As String = "SELECT DocName FROM `doctypes`;"
            Dim con As New Odbc.OdbcConnection
            Dim reader As Odbc.OdbcDataReader
            con.ConnectionString = My.Settings.con
            Dim cmd As New Odbc.OdbcCommand(SQLCommand, con)
            con.Open()
            reader = cmd.ExecuteReader()
            While reader.Read()
                Doctypes.Add(reader(0))
            End While

        Catch ex As Exception
        End Try
    End Sub
    Dim AutoCompleteCollectionUserItems As New AutoCompleteStringCollection()
    Public AutoCompleteCollectionVendor As New AutoCompleteStringCollection
    Sub FillAutoCompleteUserItems()
        Try
            Dim SQLCommand As String = "Select * FROM `records` where company = '" & My.Settings.company & "' and owner='" & My.Settings.username & "'"
            Dim con As New Odbc.OdbcConnection
            Dim reader As Odbc.OdbcDataReader

            con.ConnectionString = My.Settings.con
            Dim cmd As New Odbc.OdbcCommand(SQLCommand, con)
            con.Open()

            reader = cmd.ExecuteReader()

            color1 = 1

            While reader.Read()
                AutoCompleteCollectionUserItems.Add(reader(5))
            End While
            ComboBox1.AutoCompleteCustomSource = AutoCompleteCollectionUserItems
        Catch ex As Exception

        End Try
    End Sub
    Sub FillAutoCompleteVendor()
        Try
            Dim SQLCommand As String = "SELECT vendor FROM `vendors` WHERE `company` = '" & My.Settings.company.ToUpper & "';"
            Dim con As New Odbc.OdbcConnection
            Dim reader As Odbc.OdbcDataReader
            con.ConnectionString = My.Settings.con
            Dim cmd As New Odbc.OdbcCommand(SQLCommand, con)
            con.Open()
            reader = cmd.ExecuteReader()
            While reader.Read()
                AutoCompleteCollectionVendor.Add(reader(0))
            End While

        Catch ex As Exception
        End Try
    End Sub
    Sub CacheItems()
        Try
            Dim SQLCommand As String = "Select * FROM `records` where company = '" & My.Settings.company & "' and owner='" & My.Settings.username & "'"
            Dim con As New Odbc.OdbcConnection
            Dim reader As Odbc.OdbcDataReader

            con.ConnectionString = My.Settings.con
            Dim cmd As New Odbc.OdbcCommand(SQLCommand, con)
            con.Open()

            reader = cmd.ExecuteReader()

            color1 = 1
            While reader.Read()
                ComboBox1.Items.Add(reader(5))
            End While
        Catch ex As Exception

        End Try
    End Sub
    Sub LoadItems(Optional query As String = "")
        Try
            FlowLayoutPanel1.Controls.Clear()
            Dim SQLCommand As String = ""
            If query = "" And categorylist.SelectedIndex = 0 Then
                SQLCommand = "Select * FROM `records` where company = '" & My.Settings.company & "' and owner='" & My.Settings.username & "'"
            ElseIf query <> "" And categorylist.SelectedIndex = 0 Then
                SQLCommand = "Select * FROM `records` WHERE title LIKE '%" & query & "%' OR vendor LIKE '%" & query & "%' OR docutype LIKE '%" & query & "%' OR dateissue LIKE '%" & query & "%' OR originalfilename LIKE '%" & query & "%' AND company = '" & My.Settings.company & "' and owner='" & My.Settings.username & "'"
                'company = '" & li.company & "' and owner='" & li.username & "'"
            Else
                If ComboBox1.Text = "" Then
                    MsgBox("Invalid Query", MsgBoxStyle.Critical, "INVALID")
                    Exit Sub
                End If
                If categorylist.Text = "Title" Then
                    SQLCommand = "Select * FROM `records` where company = '" & My.Settings.company & "' and owner='" & My.Settings.username & "' and title like '%" & query & "%'"
                ElseIf categorylist.Text = "Document Type" Then
                    SQLCommand = "Select * FROM `records` where company = '" & My.Settings.company & "' and owner='" & My.Settings.username & "' and docutype like '%" & query & "%'"
                ElseIf categorylist.Text = "Date Issue" Then
                    SQLCommand = "Select * FROM `records` where company = '" & My.Settings.company & "' and owner='" & My.Settings.username & "' and dateissue like '%" & query & "%'"
                ElseIf categorylist.Text = "Filename" Then
                    SQLCommand = "Select * FROM `records` where company = '" & My.Settings.company & "' and owner='" & My.Settings.username & "' and originalfilename like '%" & query & "%'"
                ElseIf categorylist.Text = "Vendor" Then
                    SQLCommand = "Select * FROM `records` where company = '" & My.Settings.company & "' and owner='" & My.Settings.username & "' and vendor like '%" & query & "%'"
                ElseIf categorylist.Text = "Description" Then
                    SQLCommand = "Select * FROM `records` where company = '" & My.Settings.company & "' and owner='" & My.Settings.username & "' and vendor like '%" & query & "%'"

                End If
            End If


            'InputBox("ds", "asda", SQLCommand)
            Dim con As New Odbc.OdbcConnection
            Dim reader As Odbc.OdbcDataReader

            con.ConnectionString = My.Settings.con
            Dim cmd As New Odbc.OdbcCommand(SQLCommand, con)
            con.Open()

            reader = cmd.ExecuteReader()

            color1 = 1

            Dim LoadLimit As Integer = 0

            While reader.Read()
                'MsgBox(reader(4))
                If (LoadLimit >= 30) Then
                    Exit While
                End If
                'Console.WriteLine(String.Format("{0}, {1}", reader(0), reader(1)))
                Dim newitem As New viewitem
                newitem.MainForm = Me
                'Dim info As New FileInfo(reader(8))
                newitem.FtpFile = reader(9)
                newitem.Upid = reader(0)
                newitem.lbltitle.Text = reader(5)
                newitem.lblinfo.Text = reader(4) & " • " & reader(6) & " • " & reader(7) & " • "
                newitem.LabelVendor = reader(4)
                newitem.type = reader(6)
                newitem.issued = reader(7)
                newitem.DEsc = reader(10)


                newitem.ref1 = reader(13).ToString
                newitem.ref2 = reader(14).ToString
                newitem.check1 = reader(15).ToString


                '& Math.Round(info.Length / 1024) & "KB"
                'color

                ' FlowLayoutPanel1.Controls.Add(newitem)
                tempitem = newitem
                Dim Invoker As New MethodInvoker(AddressOf AddControl)
                Me.Invoke(Invoker)
                ChangeColor()
                LoadLimit += 1

            End While
        Catch ex As Exception
            'MsgBox(ex.ToString)
        End Try
    End Sub
    Dim tempitem As viewitem
    Private Sub AddControl()
        FlowLayoutPanel1.Controls.Add(tempitem)
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        LoadItems(ComboBox1.Text)
    End Sub
    Public color1 As Integer = 1
    Public Sub ChangeColor()
        Try
            For Each item In FlowLayoutPanel1.Controls
                If color1 = 1 Then
                    color1 = 0
                    item.BackColor = Color.Gainsboro
                ElseIf color1 = 0 Then
                    color1 = 1
                    item.BackColor = Color.WhiteSmoke
                End If
            Next
        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub
   

    Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            LoadItems(ComboBox1.Text)
        End If
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        Try
            For Each item As String In ViewedItem
                Dim Filesaved As New FileInfo(item)
                Filesaved.Delete()
            Next
        Catch ex As Exception
            'MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If (Button4.Text = "ASC") Then
            Button4.Text = "DESC"
        Else
            Button4.Text = "ASC"
        End If
    End Sub

    Private Sub GeneralSettingsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GeneralSettingsToolStripMenuItem.Click
        Form2.MainForm = Me
        Form2.ShowDialog()
    End Sub

    Private Sub UploadFilesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UploadFilesToolStripMenuItem.Click
        upload.Show()
    End Sub
End Class
